package com.tmall.tmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tmall.tmall.mapper.ProductMapper;
import com.tmall.tmall.pojo.*;
import com.tmall.tmall.service.OrderService;
import com.tmall.tmall.service.ShoppingcartService;
import com.tmall.tmall.mapper.ShoppingcartMapper;
import com.tmall.tmall.utils.ThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Lenovo1
 * @description 针对表【shoppingcart(购物车表)】的数据库操作Service实现
 * @createDate 2025-05-12 15:48:06
 */
@Service
public class ShoppingcartServiceImpl implements ShoppingcartService {

    @Autowired
    private ShoppingcartMapper shoppingcartMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private OrderService orderService;


    @Override
    public void save(Shoppingcart shoppingCart) {
        Shoppingcart shoppingcart = shoppingcartMapper.getByProductId(shoppingCart.getProductId());
        if (shoppingcart != null) {
            shoppingcart.setQuantity(shoppingcart.getQuantity() + 1);
            updateQuantity(shoppingcart);
            return;
        }
        shoppingcartMapper.addShoppingcart(shoppingCart);
    }

    @Override
    public PageRt page(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Shoppingcart> list = shoppingcartMapper.page(ThreadLocalUtils.getUid());
        List<ShoppingCartVO> list1 = new ArrayList<>();
        for (Shoppingcart shoppingcart : list) {
            ShoppingCartVO shoppingCartVO = new ShoppingCartVO();
            shoppingCartVO.setId(shoppingcart.getId());
            shoppingCartVO.setQuantity(shoppingcart.getQuantity());
            shoppingCartVO.setProductId(shoppingcart.getProductId());
            ProductRt byId = productMapper.getById(Math.toIntExact(shoppingcart.getProductId()));
            shoppingCartVO.setProductName(byId.getName());
            shoppingCartVO.setProductMainImg(byId.getMainImg());
            shoppingCartVO.setProductPrice(byId.getPrice());
            list1.add(shoppingCartVO);
        }
        PageInfo<ShoppingCartVO> pageInfo = new PageInfo<>(list1);
        return new PageRt(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public void deleateShoppingCart(List<Long> ids) {
        for (Long id : ids) {
            shoppingcartMapper.deleteById(id);
        }
    }

    @Override
    public void updateQuantity(Shoppingcart shoppingCart) {
        shoppingcartMapper.updateQuantity(shoppingCart.getId(), shoppingCart.getQuantity());
    }

    @Override
    public void insertOrder(ShoppingCartSettlementDTO settlementDTO) {

        List<Long> ids = settlementDTO.getIds();
        List<Products> list = new ArrayList<>();
        for (Long id : ids) {
            Shoppingcart shoppingcart = shoppingcartMapper.getById(id);
            ProductRt byId = productMapper.getById(shoppingcart.getProductId().intValue());
            Products products = new Products();
            products.setPid(Math.toIntExact(shoppingcart.getProductId()));
            products.setSum(byId.getPrice());
            products.setNumber(shoppingcart.getQuantity());
            list.add(products);
        }
        Orders orders = new Orders();
        orders.setProductList(list);
        orderService.insert(orders);

        deleateShoppingCart(settlementDTO.getIds());

    }
}




